package com.situ.company.employe.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;

import com.situ.company.employe.model.EmployeeModel;
import com.situ.company.employe.service.IEmployeeService;
import com.situ.company.employe.service.impl.EmployeeServiceImpl;

import util.FmtRequest;
import util.FmtUpload;

@WebServlet("/EmployeeUploadServlet")
public class EmployeeUploadServlet  extends HttpServlet{

	private static final long serialVersionUID=1L;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			doPost(req, resp);
	}
	private IEmployeeService service=new EmployeeServiceImpl();
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Map<String,Object> map=null;
		try {
			map=FmtUpload.upload(req);
		} catch (FileUploadException  | IOException e) {
			e.printStackTrace();
		} 
		String code=map.get("code").toString();
		List<String> filenames=(List<String>) map.get(FmtUpload.FILE_NAMES);
		String image=filenames.get(0);
		
		EmployeeModel model=new EmployeeModel(code);
		
		model.setImage(filenames.get(0));
		
		String res=service.updatepic(model);
		
		Map<String,String> result=new HashMap<String,String>();
		result.put("code", res);
		result.put("image",image);
		FmtRequest.write(resp.getWriter(),result);
		
	}
}
